package com.example.myapplication.tack_30;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tack_30.adapter.KeywordAdapter;
import com.example.myapplication.tack_30.adapter.NearAdapter;
import com.example.myapplication.tack_30.data.FenLeiData;
import com.example.myapplication.tack_30.data.KeywordData;
import com.example.myapplication.tack_30.data.NearData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class ThemeActivity extends AppCompatActivity {
    private ImageView exit;
    private Intent intent;
    private Gson gson;
    private FenLeiData.DataDTO dataDTO;
    private RecyclerView near;
    private TextView name;
    private List<NearData.RowsDTO> listNearData;
    private SearchView search;
    private TextView search_button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_31_activity_theme);
        init();
        initExit();
        Theme();
        initSearch();
    }


    private void initKeyword(String query) {
        String url = "prod-api/api/takeout/search?keyword="+query;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, KeywordData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                KeywordData keyword = (KeywordData)obj;
                if(keyword.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            near.setAdapter(new KeywordAdapter(keyword.getRows(),ThemeActivity.this));
                            near.setLayoutManager(new LinearLayoutManager(ThemeActivity.this));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initSearch() {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                submit(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                change(query);
                return true;
            }
        });
    }

    private void change(String query) {
        if(!"".equals(query) && !TextUtils.isEmpty(query)){
            initKeyword(query);
        }else{
            Theme();
        }
    }

    private void submit(String query) {
        if(!"".equals(query) && !TextUtils.isEmpty(query)){
            initKeyword(query);
        }else{
            Theme();
        }
    }

    private void Theme() {
        String url = "prod-api/api/takeout/seller/list?themeId="+dataDTO.getId();
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NearData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NearData nearData = (NearData)obj;
                if(nearData.getCode() == 200){
                    listNearData = nearData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            near.setAdapter(new NearAdapter(listNearData, ThemeActivity.this));
                            near.setLayoutManager(new LinearLayoutManager(ThemeActivity.this));
                        }
                    });

                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }


    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThemeActivity.this.finish();
            }
        });
    }

    private void init() {
        search_button = findViewById(R.id.button);
        search = findViewById(R.id.search);
        exit = findViewById(R.id.exit);
        near = findViewById(R.id.near);
        name = findViewById(R.id.name);
        intent = getIntent();
        String stringExtra = intent.getStringExtra(Common.COMMON);
        gson = new Gson();
        dataDTO = gson.fromJson(stringExtra, FenLeiData.DataDTO.class);
        name.setText(dataDTO.getThemeName());
    }
}